package com.businessportal.web.ckeditor.filebrowser;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.StringWriter;

/**
 * Created by Vadim on 6/27/14.
 */
public class InitCommand extends CommandBase {

    public InitCommand(CommandDetector.CkCommand id, CommandDetector.CkContentType type) {
        super(id, type);
    }

    @Override
    public String getRespond() {

        try {

            DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
            DocumentBuilder docBuilder = dbfac.newDocumentBuilder();
            Document doc = docBuilder.newDocument();

            //create the root element and add it to the document
            Element root = doc.createElement("Connector");
            root.setAttribute("resourceType", type.toString());

                Element error = doc.createElement("Error");
                error.setAttribute("number", "0");
                root.appendChild(error);

                Element connectorInfo = doc.createElement("ConnectorInfo");
                connectorInfo.setAttribute("enabled", "true");
                connectorInfo.setAttribute("s", "ckeditor.com");
                connectorInfo.setAttribute("c", "BHBMJQJXW");
                connectorInfo.setAttribute("thumbsEnabled", "true");
                //connectorInfo.setAttribute("thumbsUrl", "/userfiles/_thumbs/");
                connectorInfo.setAttribute("thumbsDirectAccess", "true");
                connectorInfo.setAttribute("thumbsWidth", "100");
                connectorInfo.setAttribute("thumbsHeight", "100");
                connectorInfo.setAttribute("imgWidth", "1600");
                connectorInfo.setAttribute("imgHeight", "1200");
                connectorInfo.setAttribute("uploadMaxSize", "8388608");
                connectorInfo.setAttribute("uploadCheckImages", "false");
                connectorInfo.setAttribute("plugins", "imageresize,fileeditor");
                root.appendChild(connectorInfo);

                Element resourceTypes = doc.createElement("ResourceTypes");

                    //<ResourceType name="Images" url="/userfiles/images/" allowedExtensions="bmp,gif,jpeg,jpg,png,avi,iso,mp3"
                    // deniedExtensions="" hash="408961dd4a0d6ef9" hasChildren="true" acl="17" maxSize="8388608" />
                    Element resourceType = doc.createElement("ResourceType");
                    resourceType.setAttribute("name", "Images");
                    resourceType.setAttribute("url", "/userfiles/images/");// !!!!
                    resourceType.setAttribute("allowedExtensions", "bmp,gif,jpeg,jpg,png,avi,iso,mp3");
                    resourceType.setAttribute("deniedExtensions", "");
                    resourceType.setAttribute("hash", "408961dd4a0d6ef9");
                    resourceType.setAttribute("hasChildren", "true");
                    resourceType.setAttribute("acl", "17");
                    resourceType.setAttribute("maxSize", "8388608");

                resourceTypes.appendChild(resourceType);
                root.appendChild(resourceTypes);

                Element pluginsInfo = doc.createElement("PluginsInfo");

                    //<imageresize smallThumb="90x90" mediumThumb="120x120" largeThumb="180x180" />
                    Element imageresize = doc.createElement("imageresize");
                    imageresize.setAttribute("smallThumb", "90x90");
                    imageresize.setAttribute("mediumThumb", "120x120");
                    imageresize.setAttribute("largeThumb", "180x180");

                pluginsInfo.appendChild(imageresize);
                root.appendChild(pluginsInfo);

            doc.appendChild(root);

            //set up a transformer
            TransformerFactory transfac = TransformerFactory.newInstance();
            Transformer trans = transfac.newTransformer();
            trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
            trans.setOutputProperty(OutputKeys.INDENT, "yes");

            //create string from xml tree
            StringWriter sw = new StringWriter();
            StreamResult result = new StreamResult(sw);
            DOMSource source = new DOMSource(doc);
            trans.transform(source, result);

            return sw.toString();

        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (TransformerConfigurationException e) {
            e.printStackTrace();
        } catch (TransformerException e) {
            e.printStackTrace();
        }

        return null;
    }
}
